import { grids } from '../../utils/grids.js';

const app = getApp();

const pageData = {
  data: {
    width: 0,
    score: '01',
    colors: [],
    cube: 2,
    seconds: 60,
    pause: false,
    closeNumber: false,
    gameover: false
  },

  onLoad: function () {
    this.data.countAudio = wx.createInnerAudioContext();
    this.data.countAudio.src = 'https://resource.160nurse.com/canvas/healthy/mp3/countdown.mp3';
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          width: parseFloat(res.windowWidth / 100).toFixed(2)
        });
      }
    });
  },

  onShow: function () {
    let that = this
    let number = 3
    that.data.countAudio.autoplay = true;
    that.data.countAudio.onPlay(() => {
      console.log('开始播放')
    });
    that.data.countAudio.onEnded(() => {
      console.log('播放结束')
      that.data.countAudio.destroy()
    });
    let timer = setInterval(() => {
      if (number >= 1) {
        that.setData({
          number: number
        })
        number--
      } else {
        clearInterval(timer)
        that.setData({
          closeNumber: true
        })
        that._ren();
        that._timer();
      }
    }, 1000)
  },

  onUnload: function () {
    this.setData({
      pause: true
    });
    this._timer();
  },

  // 下一关
  goNext: function (e) {
    let that = this
    let next = e.currentTarget.dataset.next;
    let click = wx.createInnerAudioContext();
    click.src = 'https://resource.160nurse.com/canvas/healthy/mp3/click.mp3';
    click.autoplay = true
    click.onPlay(() => {
      console.log('开始播放')
    });
    click.onEnded(() => {
      console.log('播放结束')
      click.destroy()
    });
    if (next) {
      this.setData({
        score: parseInt(this.data.score) + 1 < 10 ? '0' + (parseInt(this.data.score) + 1) : parseInt(this.data.score) + 1
      });
      this._ren();
    }
  },

  // 暂停
  toPause: function () {
    this.setData({
      pause: true
    });
    this._timer();
  },

  // 继续
  toContinue: function () {
    this.setData({
      pause: false
    });
    this._timer();
  },

  // 计时器
  _timer: function () {
    let tm = setInterval(() => {
      let scnd = this.data.seconds;

      // 暂停
      if (this.data.pause) {
        clearInterval(tm);
        return;
      }

      // 结束
      if (parseInt(scnd) == 0) {
        clearInterval(tm);
        this.setData({
          seconds: '00',
          gameover: true
        });
        this._gameOver();
        return;
      }

      this.setData({
        seconds: scnd - 1 < 10 ? '0' + (scnd - 1) : scnd - 1
      });
    }, 1000);
  },

  // 游戏结束
  _gameOver: function () {
    let score = parseInt(this.data.score) - 1;
    let levels = ["“色”彩盲侠", "不“色”君子", "好“色”之徒", "“色”艺绝伦"];
    let descs = ["眼前的黑不是黑，你说的白是什么白", "色即是空，阿弥陀佛", "做一个俗人，贪财好色", "再美的色彩也逃不过我的魔法"]
    let idx = 8 > score ? 0 : Math.ceil((score - 8) / 8) > 3 ? 3 : Math.ceil((score - 8) / 8);
    console.log(idx, descs[idx])
    let level = levels[idx] || levels[levels.length - 1];
    let gameover = wx.createInnerAudioContext()
    gameover.src ="https://resource.160nurse.com/canvas/healthy/mp3/gameover.mp3"
    gameover.autoplay = true
    gameover.onPlay(() => {
      console.log('开始播放')
    })
    gameover.onEnded(() => {
      console.log('结束')
      setTimeout(() => {
        wx.redirectTo({
          url: 'color_over?level=' + level + '&score=' + score + '&desc=' + descs[idx]
        });
      }, 1500)
    })
  },

  // 数据准备 & 渲染
  _ren: function () {
    let width = this.data.width;
    let score = parseInt(this.data.score) - 1;
    let round = grids(score);
    let cube = round.cube;
    let total = cube * cube;
    let colorAry = new Array(total);
    let size = Math.floor(width * 75.47 / cube);

    colorAry.fill({
      color: round.mainColor,
      next: false,
      size: size
    }, 0, colorAry.length);

    let oneIndex = Math.floor(Math.random() * total);
    console.log(oneIndex)
    colorAry[oneIndex] = {
      color: round.oneColor,
      next: true,
      size: size
    };

    this.setData({
      cube: cube,
      colors: colorAry
    });
  },
  // 重来
  replay() {
    wx.redirectTo({
      url: 'color_index',
    })
  }
};

Page(pageData);